<?php

namespace Frieza\Modules\ContentCategory\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\ContentCategory\ContentCategoryDimension;
use Illuminate\Support\MessageBag;





class DimensionController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    //维度列表
    public function index(Request $request, Content $content)
    {

        $request = $request->all();
        return $content
            ->header('内容分类维度管理')
            ->breadcrumb(
                ['text' => '内容分类维度列表', 'url' => '/contentCategory/dimension/index']
            )
            ->body(self::listAction($request));


    }

    public function listAction($request = [])
    {
        $infos = ContentCategoryDimension::getList();
        $gridModel = new pGridDataModel($infos, count($infos), []);

        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disablePagination();
            $grid->disableFilter();
            $grid->disableRowSelector();
            $grid->disableActions();

            $grid->column('id');
            $grid->column('dimension', '维度');
            $grid->column('addtime', '添加时间');
        });

        $list = $listGrid->render();

        return $list;
    }

    public function add(Request $request, Content $content)
    {

        $request = $request->all();
        return $content
            ->header('内容分类维度管理')
            ->breadcrumb(
                ['text' => '内容分类维度管理', 'url' => '/contentCategory/dimension/index'],
                ['text' => '添加维度', 'url' => '/contentCategory/dimension/index/create']
            )
            ->body(self::editAction($request, $id = 0));

    }

    public function editAction($request, $id = 0)
    {
        $data = [];
        if($id == 0) {
            //添加
            $form = new Form($data);
        }
        $form->hidden('id');
        $form->text('dimension', '维度名称')->setWidth(4, 2)->required('不能为空')->help('请输入维度名称');
        $form->action('/admin/contentCategory/dimension/index/save');

        return $form->render();
    }

    public function editSubmit(Request $request, Content $content)
    {
        $request = $request->all();
        $rs = false;

        $data['dimension'] = $request['dimension'];
        $data['admin_id'] = Admin::user()->id;
        $data['dimension_status'] = ContentCategoryDimension::STATUS_NORMAL;
        $data['addtime'] = date('Y-m-d H:i:s', time());

        $rs = ContentCategoryDimension::addInfo($data);


        if($rs) {
            $success = new MessageBag([
                'title' => '提交成功'
            ]);
            return redirect('/admin/contentCategory/dimension/index')->with(compact('success'));
        } else {
            $error = new MessageBag([
                'title' => '提交失败'
            ]);
            return redirect('/admin/contentCategory/dimension/index')->with(compact('error'));
        }


    }

}